import axios from 'axios'
// 实例化axios对象
let service = axios.create({
    baseURL: process.env.REACT_APP_BASE_URL,
    timeout: 10 * 1000
})

// 设置请求拦截器
service.interceptors.request.use((config) => {
    // 设置请求头
    config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
    // 设置第二个请求头
    // config.headers['x-token'] = localStorage.getItem('token');
    return config
}, (error) => {
    return Promise.reject(error)
})

// 设置响应拦截器
service.interceptors.response.use((res) => {
    return res
}, (error) => {
    if (error.response.status === 401) {
        console.log('身份认证过期，请重新登录')
    }
    else if (error.response.status === 404) {
        console.log('资源路径有误')
    }
    else if (error.response.status === 500) {
        console.log('服务内部错误')
    }
    return Promise.reject(error)
})

export default service;